<template>
	<view class="my">
		<view class="top">
			<view class="box" v-if="hasLogin">
				<view @click="toUserInfo" class="userinfo">
					<view class="avatar">
						<image :src="userInfo.avatar_file?userInfo.avatar_file.url:'../../static/avatar.webp'" mode="aspectFill"></image>
					</view>
					<view class="info">
						<view class="nickname">
							{{userInfo.nickname || userInfo.username}}
						</view>
						<view class="register-time">
							{{$u.timeFrom(userInfo.register_date, 'yyyy年mm月dd日 hh时MM分ss秒')}}注册
						</view>
					</view>
				</view>
				<view class="more">
					<text class="iconfont icon-gengduo1"></text>
				</view>
			</view>
			<view class="nologin" v-else>
				<view class="avatar">
					<image src="../../static/avatar.webp" mode="aspectFill"></image>
				</view>
				<view @click="toLogin" class="btn">
					点击登录
				</view>
			</view>
			
			<view class="bg">
				<image :src="(hasLogin && userInfo.avatar_file)?userInfo.avatar_file.url:'../../static/avatar.webp'" mode="aspectFill"></image>
			</view>
		</view>
		<view class="content">
			<view class="card">
				<view class="card-item"><text class="num">{{attrObj.likeNum || 0}}</text>获赞</view>
				<view class="card-item"><text class="num">{{attrObj.commentNum || 0}}</text>评论</view>
				<view class="card-item"><text class="num">{{attrObj.artNum || 0}}</text>发文</view>
			</view>
			<view class="list">
				<view @click="itemClick(item)" class="item" :class="{'border':item.borderLine}" v-for="item in list">
					<view class="left">
						<text class="iconfont" :class="item.icon"></text>
					</view>
					<view class="center">
						<text class="text">{{item.name}}</text>
					</view>
					<view class="right">
						<text class="iconfont icon-gengduo1"></text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const db = uniCloud.database()
	import {store,mutations} from '../../uni_modules/uni-id-pages/common/store.js'
	export default {
		data() {
			return {
				attrObj:{
					likeNum:0,
					commentNum:0,
					artNum:0
				},
				list:[
					{icon:'icon-wenzhang-copy',name:'我的文章',path:"/pages/my/my-article/my-article"},
					{icon:'icon-dianzan',name:'我的点赞',path:"/pages/circle-like/list"},
					{icon:'icon-xinxi',name:'评论过的',borderLine:true},
					{icon:'icon-guanyu',name:'关于'},
					{icon:'icon-yijianfankui',name:'意见反馈',borderLine:true,path:"/uni_modules/uni-feedback/pages/opendb-feedback/opendb-feedback"},
					{icon:'icon-tuichu',name:'退出登录'},
				]
			};
		},
		computed:{
			hasLogin(){
				return store.hasLogin
			},
			userInfo(){
				return store.userInfo
			}
		},
		onLoad() {
			console.log(this.userInfo)
			this.getAttrCount()
		},
		methods:{
			// 获取获赞、评论、发文数量
			async getAttrCount(){
				if(!this.hasLogin) return;
				let artCountObj = await db.collection("circle-articles").where(`user_id==$cloudEnv_uid`).count();
				this.attrObj.artNum = artCountObj.result.total;
				let likeObj = await db.collection("circle-articles").where(`user_id==$cloudEnv_uid`).groupBy('like_count').groupField('sum(like_count) as likeNum').get();
				this.attrObj.likeNum = likeObj.result.data[0].likeNum
			},
			// to 个人资料
			toUserInfo(){
				uni.navigateTo({
					url:"/uni_modules/uni-id-pages/pages/userinfo/userinfo"
				})
			},
			// to 登录
			toLogin(){
				uni.navigateTo({
					url:"/uni_modules/uni-id-pages/pages/login/login-withpwd"
				})
			},
			itemClick(item){
				console.log(item.name)
				if(!this.hasLogin){
					return uni.showToast({
						title:"您还未登录",
						icon:"none"
					})
				}
				if(item.path){
					uni.navigateTo({
						url:item.path
					})
				}
				if(item.name=='退出登录'){
					uni.showModal({
						title:"提示",	
						content:"是否确认退出登录",
						success:res=> {
							if(res.confirm){
								mutations.logout()
							}
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.my{
		.top{
			height: 300rpx;
			display: flex;
			align-items: center;
			padding: 0 30rpx;
			padding-top: var(--status-bar-height);
			position: relative;
			.box{
				display: flex;
				align-items: center;
				justify-content: space-between;
				color: #fff;
				position: relative;
				z-index: 10;
				width: 100%;
				.userinfo{
					flex: 1;
					display: flex;
					align-items: center;
					.avatar{
						width: 120rpx;
						height: 120rpx;
						border-radius: 50%;
						overflow: hidden;
						image{
							width: 100%;
							height: 100%;
						}
					}
					.info{
						margin-left: 20rpx;
						flex: 1;
						overflow: hidden;
						color: #fff;
						.nickname{
							font-size: 32rpx;
							font-weight: bold;
						}
						.register-time{
							font-size: 24rpx;
							margin-top: 5rpx;
						}
					}
				}
				.more{
					.iconfont{
						font-size: 38rpx;
						font-weight: bold;
					}
				}
			}
			.nologin{
				display: flex;
				flex-direction: column;
				align-items: center;
				color: #fff;
				position: relative;
				z-index: 10;
				width: 100%;
				.avatar{
					width: 120rpx;
					height: 120rpx;
					border-radius: 50%;
					overflow: hidden;
					image{
						width: 100%;
						height: 100%;
					}
				}
				.btn{
					margin-top: 20rpx;
					width: 200rpx;
					height: 60rpx;
					border-radius: 30rpx;
					background-color: #0199FE;
					color: #fff;
					font-size: 28rpx;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
			.bg{
				position: absolute;
				left: 0;
				top: 0;
				width: 100%;
				height: 100%;
				overflow: hidden;
				image{
					width: 100%;
					height: 100%;
					// filter:blur(20px);
					// transform: scale(2);
					opacity: 0.5;
				}
			}
		}
		.content{
			border-radius: 30rpx;
			transform: translateY(-30rpx);
			background-color: #fff;
			.card{
				display: flex;
				align-items: center;
				justify-content: space-around;
				padding: 30rpx;
				&-item{
					color: #888;
					font-size: 28rpx;
					.num{
						font-weight: bold;
						color: #333;
						margin-right: 5rpx;
					}
				}
			}
			.list{
				.item{
					display: flex;
					align-items: center;
					padding: 30rpx 30rpx;
					color: #333;
					&.border{
						border-bottom: 10rpx solid #f7f7f7;
					}
					.left{
						.iconfont{
							font-size: 38rpx;
							color: #000;
						}
					}
					.center{
						flex: 1;
						margin: 0 10rpx;
						font-size: 28rpx;
					}
					.right{
						.iconfont{
							font-size: 28rpx;
							color: #333;
						}
					}
				}
			}
		}
	}
</style>
